var ns, bmLocation,toast;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            ID:0,
            pageindex: 0,
            pagesize: 10,
            // status: 1, // 1-任务列表 2-新增记录
            PlanType: 1,
            TaskStatus:0,//0-未开始,1-进行中,2-已结束
            planTypeList:[1,2],
        },
        total: {
            ProcessCount: 0,
        },
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showTop:false,
        showsearch:false,
        number:0,
        selectlist:[],
        showsearchresult:false,
        searchresult:'',
        checkbox:false,
        checked:false,
        Read:false,
        showWin:false,
        isShowRead:false,
        touchID:0,//长按选中id

    },
    methods: {
        get_data: function() {
            var that = this;
            that.is_searching= true,
            that.can_scroll = false;
            var options = {};
            options.action = "getjpushlogdeviceplantasklist";
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.planTypeList =JSON.stringify(that.form.planTypeList);
            options.IsReadonly=that.Read;
            if (that.form.pageindex >= 10){
                toast.loading({
                    title: "提交中",
                    duration: 2000
                }, function(ret) {});
            }
            ns.post(options, function(succeed, data, err) {
                that.is_searching= false;
                if (succeed) {
                    // if (data.list.length == that.form.pagesize) {
                    //     that.can_scroll = true;
                    // }
                    // if (that.form.pageindex == 0) {
                    //     that.list =[];
                    //     setTimeout(() => {
                    //         window.scrollTo(0,document.body.scrollHeight);
                    //     }, 300);
                    // }
                    // data.list.forEach(function(item){
                    //     that.list.splice(0,0,item);
                    // })
                    // if(that.form.pageindex >= 10){
                    //     setTimeout(() => {
                    //         that.do_scrollbtn(that.list[0].JpushLogID);
                    //         toast.hide();
                    //     }, 100);
                    // }

                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.list;
                    } else {
                        that.list = that.list.concat(data.list);
                    }

                    app.current_item_length = that.list.length
                    that.total.ProcessCount = data.ProcessCount;
                    that.number=data.number;
                    for(var i=0;i<data.list.length;i++){
                        data.list[i].JpushAddTime = ns.SetTime_style(data.list[i].JpushAddTime);
                    }
                    if(that.form.ID){
                        setTimeout(function() {
                            that.do_scrollTo();
                        }, 500)
                    }
                    api.sendEvent({
                        name:'isreadEquipment'
                    })


                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        choose_noread:function(){
            var that=this;
            that.Read=!that.Read;
            that.get_data();
            // that.checked=!that.checked;
            // console.log(JSON.stringify(that.checked));
        },
        open_renwu:function(){
            ns.openWin('equipment_renwulist', '设备巡检待办');
        },
        //点击遮罩关闭弹窗
        doHideWin:function(){
            var that=this;
            that.showWin=false;
            that.touchID=0;
            that.get_data();
        },
        //长按弹出操作框
        gettouchStart:function(index,JpushLogID,IsRead){
            var that = this;
            api.addEventListener({
                name:'longpress'
            }, function(ret){  
                that.showWin=!that.showWin; 
                if(!IsRead){
                    that.isShowRead=true;
                }else{
                    that.isShowRead=false;
                }
                that.touchID=JpushLogID;
            })
        },
        do_Read:function(){
            var that = this;
            var options={};
            options.action="jpushlogusersetread";
            options.JpushLogID=that.touchID;
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.showWin=false;
                    that.get_data();
                }
            });
        },
        //删除消息
        deleteItem:function(){
            var that = this;
            var options={};
            options.action="doremovepushmsg";
            options.ID=that.touchID;
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.showWin=false;
                    that.get_data();
                }
            });
        },
        //全部标记为已读
        All_read:function(){
            var that=this;
            var options={};
            options.action="deviceplanmessageread";
            options.Type=1;
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.get_data();
                }
            });
        },
        //翻页第一条数据的位置
        do_scrollbtn:function(id){
    　　　　 var sollid = document.getElementById('scroll'+id);
    　　　　 window.scrollTo(0,sollid.offsetTop); 
    　　},
        select_status: function(status) {
            var that = this;
            that.showTop=false;
            that.form.pageindex=0;
            that.showsearchresult=true;
            if(status==1){
                that.searchresult="设备巡检";
                that.form.planTypeList=[1];
            }
            if(status==2){
                that.searchresult="设备维保";
                that.form.planTypeList=[2];
            }
            that.list = [];
            that.get_data();
        },
        open_list: function(item) {
            var that = this;
            var options={};
            options.action="jpushlogusersetread";
            options.JpushLogID=item.JpushLogID;
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.get_data();
                }
            });
            var url = 'equipment_list_frm';
            var title = item.PlanTypeDesc;
            ns.openWin(url, title, {
                id: item.ID
            });
        },
        open_select:function(){
            var that=this;
            that.showTop=!that.showTop;
        },
        open_add: function () {
            var that = this;
            ns.openWin('equipment_addfrm', '新增记录');
        },
        doHideSearch:function(){
            var that=this;
            that.showTop=false;
            that.showsearch=false;
        },
        do_closearch:function(){
            var that=this;
            that.showsearch=false;
            that.showTop=false;
            that.showsearchresult=false;
            that.form.planTypeList=[1,2];
            that.list = [];
            that.get_data();

        },
        select_allstatus:function(){
            var that=this;
            that.showsearch=!that.showsearch;
            // that.form.planTypeList=[1,2];
            var options={};
            options.planTypeList = JSON.stringify(that.form.planTypeList);
            options.action = "getdeviceplantaskpendinghandlelist";
            ns.post(options, function (succeed, data, err) {

                if(succeed){
                    that.selectlist=data.list;
                    
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            });
        },
        do_scrollTo:function(){
            var that = this;
    　　　　 var sollid = document.getElementById('scroll'+that.form.ID);
    　　　　 window.scrollTo(0,sollid.offsetTop); 
            var classlist=document.getElementById('scroll'+that.form.ID).getElementsByClassName('boxbg')[0];
            classlist.style.backgroundColor = "#eaf1ff";
            setTimeout(function() {
                classlist.style.backgroundColor = "#f7fbff";
            }, 800)
            
    　　},

        //时间处理
        // SetTime_style:function(time){
        //     var lsDate=new Date(time);
        //     var nowDate=new Date();

        //     var nowY=nowDate.getFullYear();
        //     var nowM=nowDate.getMonth()+1;
        //     var nowD=nowDate.getDate();
        //     var nowH=nowDate.getHours();

        //     var Y = lsDate.getFullYear();
        //     var M = lsDate.getMonth() + 1 < 10 ? '0' + (lsDate.getMonth() + 1) : lsDate.getMonth() + 1;
        //     var D = lsDate.getDate() < 10 ? '0' + lsDate.getDate() : lsDate.getDate();
        //     var H = lsDate.getHours() < 10 ? '0' + lsDate.getHours() : lsDate.getHours();
        //     var S = lsDate.getMinutes() < 10 ? '0' + lsDate.getMinutes() : lsDate.getMinutes();

        //     if(Y == nowY){
        //         if (M == nowM) {
        //             if(D == nowD){
        //                 if (H < 12) {
        //                     return '上午 ' + H + ':' + S;
        //                 }else{
        //                     return '下午 ' + H + ':' + S;
        //                 }  
        //             }
        //             else if(D == nowD-1){
        //                 return '昨天 ' + H + ':' + S;
        //             }
        //             else{
        //                 return M + '-' + D + ' ' + H + ':' + S;
        //             }
        //         }else{
        //            return M + '-' + D + ' ' + H + ':' + S;
        //         }
        //     }
        //     else{
        //         return Y + '-' + M + '-' + D + ' ' + H + ':' + S;
        //     }
            
        // },
       
    },
    
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.form.ID = api.pageParam.ID || 0;
    app.get_data();
    // api.addEventListener({
    //     name: 'devicetaskcomplete'
    // }, function(ret, err) {
    //     app.get_data();
    // });
    // api.addEventListener({
    //     name: 'startplantaskdone'
    // }, function(ret, err) {
    //     app.get_data();
    // });
    api.addEventListener({
        name: 'do_open_add'
    }, function (ret, err) {
        app.open_add();
    });
    api.addEventListener({
        name: 'do_open_select'
    }, function(ret, err) {
        app.open_select();
    });
    // ns.init_pull(true,true);
    ns.init_pull();
    //清空
    api.addEventListener({
        name: 'do_open_delete'
    }, function (ret, err) {
        api.confirm({
            title: '提示信息',
            msg: '确认要清理所有的消息信息吗？',
            buttons: ['确定', '取消']
        }, function(ret, err) {
            if (ret) {
                if (ret.buttonIndex == 1) {
                    var options={};
                    options.action="doremoveallpushmsg";
                    options.Type=3;//1-安防巡更，2-绿化保洁,3-设备巡检
                    ns.post(options, function(succeed, data, err) {
                        if (succeed) {
                            app.get_data();
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: true,
                        toastmsg: '删除中'
                    });
                }
            }
        })
    });
};

